// component/homeQuery/homeQuery.js
import ajax from "../../utils/index"
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    formData: Object,
  },

  /**
   * 组件的初始数据
   */
  data: {
    priceButArr: [['¥0-2', '¥2-4', '¥4-10', '¥10-15',], ['¥15-20', '', '', '']],
    areaButArr: [['0-50㎡', '50-100㎡', '100-200㎡', '200-500㎡',], ['500-1000㎡', '1000以上㎡', '', '']],
    topHeight: 0,
    leftElement: 0,
    rightElement: 100,
    leftTowElement: 0,
    rightTowElement: 100,
    element: null,
    userLocation: false,
    countyList: [], // 区列表
    queryData: {
      areaIndex: null, // 面积按钮下标
      priceIndex: null,// 价格按钮下标
      areaMin: 0, // 面积最小值
      areaMax: 50000,// 面积最大值
      priceMin: 0, // 价格最小值
      priceMax: 20,// 价格最大值
      housingAreaLeft: 0, // 最小值面积滑块
      housingAreaRight: 100,// 最大值面积滑块
      housingPriceLeft: 0, // 最小值价格滑块
      housingPriceRight: 100,// 最大值价格滑块
      housingAreaMax: 50000, //房源面积范围
      housingAreaMin: 0, //房源面积范围 
      housingPriceMax: 20, //房源价格范围 
      housingPriceMin: 0, //房源价格范围 
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 重置事件
     */
    resetEvent() {
      this.setData({
        queryData: {
          areaIndex: null, // 面积按钮下标
          priceIndex: null,// 价格按钮下标
          areaMin: 0, // 面积最小值
          areaMax: 50000,// 面积最大值
          priceMin: 0, // 价格最小值
          priceMax: 20,// 价格最大值
          housingAreaLeft: 0, // 最小值面积滑块
          housingAreaRight: 100,// 最大值面积滑块
          housingPriceLeft: 0, // 最小值价格滑块
          housingPriceRight: 100,// 最大值价格滑块
          housingAreaMax: 5000, //房源面积范围
          housingAreaMin: 0, //房源面积范围 
          housingPriceMax: 20, //房源价格范围 
          housingPriceMin: 0, //房源价格范围 
          sign: 1
        }
      })
    },
    /**
     * 排序点击事件
     */
    sortEvent(event) {
      const sort = event.currentTarget.dataset.index
      this.data.formData.sort = sort
      this.triggerEvent('change', this.data.formData)
    },
    /**
     * 区域点击事件
     */
    countyEvent(event) {
      const item = event.currentTarget.dataset.item
      this.data.formData.area = item.id == -1 ? '' : item.regionName
      this.triggerEvent('change', this.data.formData)
    },
    /**
     * 根据 区code 获取 区
     */
    async getCountyList() {
      const { formData } = this.data
      const res = await ajax.get(`/region/adCade${formData.adCadeCity ? '?adCadeCity=' + formData.adCadeCity : '?adCade=' + formData.adCade}`);
      res.rows.unshift({ regionName: '全部', id: -1 })
      this.setData({ countyList: res.rows })
    },
    /**
     * 提交事件
     */
    onSubmit() {
      this.triggerEvent('change', this.data.queryData)
    },
    /**
     * tags 切换事件
     */
    tagActiveEvent(event) {
      const queryActive = event.currentTarget.dataset.index
      this.triggerEvent('activEvent', queryActive || null)
      queryActive == 2 ? this.getCountyList() : null
    },
    /**
     * 面积/价格 滑块开始事件
     */
    async buttonOneStart(event) {
      this.data.element = await this.getElement('progress')
    },
    /**
     * 面积/价格 滑块移动事件
     */
    async buttonOneMove(event) {
      const { key, state } = event.currentTarget.dataset
      const { element } = this.data
      const { housingAreaLeft, housingAreaRight, housingPriceLeft, housingPriceRight, areaMin, areaMax, priceMin, priceMax } = this.data.queryData
      const pageX = event.touches[0].pageX
      const left = parseInt(pageX / element.width * 100)
      const width = parseInt(16 / element.width * 100)
      if (state == 'one') {
        const rightValue = key == 'housingPriceLeft' ? housingPriceRight : housingAreaRight
        if (left < 0 || left > 100 || left > (rightValue - width)) return
        const value = parseInt(left - width < 0 ? 0 : left - width)
        this.data.queryData[key] = value
        this.data.queryData[key == 'housingPriceLeft' ? 'housingPriceMin' : 'housingAreaMin'] = parseInt((key == 'housingPriceLeft' ? priceMax : areaMax) * (value / 100))
        this.setData({ queryData: this.data.queryData })
      } else if (state == 'tow') {
        const rightValue = key == 'housingAreaRight' ? housingAreaLeft : housingPriceLeft
        if (left < (width * 2) || left > 100 || (rightValue != 0 && left < rightValue + (width * 2))) return
        
        const value = parseInt(left)
        this.data.queryData[key] = value
        this.data.queryData[key == 'housingAreaRight' ? 'housingAreaMax' : 'housingPriceMax'] = parseInt((key == 'housingAreaRight' ? areaMax : priceMax) * (value / 100))
        this.setData({ queryData: this.data.queryData })
      }
    },

    /**
     * 获取元素宽度
     */
    getElement(id) {
      return new Promise((resolve, reject) => wx.createSelectorQuery().in(this).select(`#${id}`).boundingClientRect((rect) => !rect ? reject() : resolve(rect)).exec())
    },
    /**
     * 面积/价格按钮事件
     */
    butEvent(event) {
      const { item, key, index } = event.currentTarget.dataset
      if (!item) return;
      this.data.queryData.sign = item == '1000以上㎡' ? 2 : 1
      let strArr = item.split('-').map(item => parseInt(item.replace('¥', '')))
      strArr = strArr.length == 1 ? ['0', ...strArr] : strArr
      const list = key == 'area' ? ['areaMin', 'areaMax'] : ['priceMin', 'priceMax']
      list.forEach((item, index) => this.data.queryData[item] = strArr[index])
      key == 'area' ? (this.data.queryData.areaIndex = index, this.data.queryData.housingAreaMax = this.data.queryData.areaMax, this.data.queryData.housingAreaMin = this.data.queryData.areaMin, this.data.queryData.housingAreaLeft = 0, this.data.queryData.housingAreaRight = 100) : (this.data.queryData.priceIndex = index, this.data.queryData.housingPriceMax = this.data.queryData.priceMax, this.data.queryData.housingPriceMin = this.data.queryData.priceMin, this.data.queryData.housingPriceLeft = 0, this.data.queryData.housingPriceRight = 100);
      
      console.log()
      this.setData({ queryData: this.data.queryData })
    },
    /**
    * 查看授权
    */
    async getSetting() {
      const setting = await ajax.getSetting()
      this.setData({ userLocation: setting['scope.userLocation'] })
    },
  },
  /**
   * 生命周期
   * 在组件实例进入页面节点树时执行
   */
  lifetimes: {
    attached: function () {
      this.getSetting()
      const { formData, queryData } = this.data
      this.setData({ topHeight: ajax.getHeadlineHeight() })
      formData.queryActive == 2 ? this.getCountyList() : null
      Object.keys(queryData).forEach(item => {
        if (formData[item]) this.setData({ ['queryData.' + item]: formData[item] })
      })
    }
  }
})
